package com.web.controller.basic;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.web.dao.entity.SysUser;
import com.web.dao.impl.SequenceDao;
import com.web.dao.impl.SysUserDao;

@Controller
@RequestMapping(value="/basic/DataController/")
public class DataController {
	
	@Resource(name="SysUserDao")
	private SysUserDao sysUserDao;
	
	@Resource(name="SequenceDao")
	private SequenceDao sequenceDao;
	
	@RequestMapping(value="deleteUser.action")
	public @ResponseBody String deleteUser(@RequestParam("ids") String ids) {
		String[] idLists = StringUtils.split(ids, ",");
		for(String id : idLists) {
			sysUserDao.deleteById(Integer.parseInt(id));
		}
		
		JSONObject retObj = new JSONObject();
		retObj.put("result", "success");
		return retObj.toString();
	}
	
	@RequestMapping(value="addUser.action")
	public @ResponseBody String addUser(SysUser ety) {
		int seq = sequenceDao.getNextSeq("sysuser");
		ety.setId(seq);
		sysUserDao.insert(ety);
		
		JSONObject retObj = new JSONObject();
		retObj.put("result", "success");
		return retObj.toString();
	}

	@RequestMapping(value="searchUsers.action")
	public @ResponseBody String searchUsers() {
		JSONObject retObj = new JSONObject();
		JSONArray dataArr = new JSONArray();
		
		List<SysUser> dataList = sysUserDao.selectByEty(null);
		dataArr.addAll(dataList);
		retObj.put("result", "success");
		retObj.put("data", dataArr);
		return retObj.toString();
	}
	
	@ExceptionHandler
	public @ResponseBody String handle(Exception e) {
		e.printStackTrace();
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		obj.put("result","error");
		obj.put("info",e.getMessage());
		return obj.toString();
	}
}
